package com.example.springboot.controller;

import com.example.springboot.common.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.User;
import com.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@CrossOrigin      //解决跨域
@RestController
@RequestMapping("/user")    //表示这个Controller是专门给user用的,一级路由一定要写对
/**
 * 功能：
 * 作者：旺旺威化饼
 * 日期：2025/03/04 10:25
 **/
public class UserController {
    @Autowired
    UserService userService;
    /**
     * 新增用户信息
     * */
    @PostMapping("/add")            //http://localhost:9090/user/add
    public Result add(@RequestBody User user){
        try{
            userService.insertUser(user);
        }catch(Exception e){
            if(e instanceof DuplicateKeyException){
                return Result.error("插入数据错误");
            }else{
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }
    /**
     * 修改用户信息
     * */
    @PutMapping("/update")              //http://localhost:9090/user/update
    public Result update(@RequestBody User user){
        userService.updateUser(user);
        return Result.success();
    }
    /**
     * 单个删除用户信息
     * */
    @DeleteMapping("/delete/{id}")              //http://localhost:9090/user/delete/{id}
    public Result delete(@PathVariable Integer id){
        userService.deleteUser(id);
        return Result.success();
    }
    /**
     * 批量删除用户信息
     * */
    @DeleteMapping("/delete/batch")              //http://localhost:9090/user/delete/batch
    public Result batchDelete(@RequestBody List<Integer> ids){
        userService.batchDeleteUser(ids);
        return Result.success();
    }
    /**
     * 查询全部用户信息
     * */
    @GetMapping("/selectAll")              //http://localhost:9090/user/selectAll
    public Result selectAll(){
        List<User> userList = userService.selectAll();
        return Result.success(userList);
    }
    /**
     * 根据ID查询用户信息
     * */
    @GetMapping("/selectById/{id}")              //http://localhost:9090/user/selectById/{id}
    public Result selectById(@PathVariable Integer id){
        User user= userService.selectById(id);
        return Result.success(user);
    }
    /**
     * 根据name查询用户信息
     * 根据条件查询的时候，如果不确定查询的结果有几个，那就统一返回list对象集合，这样最为稳妥不会报错
     * */
    @GetMapping("/selectByName/{name}")              //http://localhost:9090/user/selectByName/{name}
    public Result selectByName(@PathVariable String name){
        List<User> userList= userService.selectByName(name);
        return Result.success(userList);
    }
    /**
     * 多条件查询用户信息
     * */
    @GetMapping("/selectByMore")              //http://localhost:9090/user/selectByMore?username=旺旺威化饼&name=威化饼
    public Result selectByMore(@RequestParam String username,@RequestParam String name){
        List<User> userList=userService.selectByMore(username,name);
        return Result.success(userList);
    }
    /**
     * 多条件模糊查询用户信息
     * */
    @GetMapping("/selectByVague")              //http://localhost:9090/user/selectByVague?username=旺旺威化饼&name=威化饼
    public Result selectByVague(@RequestParam String username,@RequestParam String name){
        List<User> userList=userService.selectByVague(username,name);
        return Result.success(userList);
    }
    /**
     * 分页模糊查询用户信息
     * */
    @GetMapping("/selectByPage")              //http://localhost:9090/user/selectByVague?username=旺旺威化饼&name=威化饼
    public Result selectByPage(@RequestParam Integer pageNum,   //当前页码
                               @RequestParam Integer pageSize,  //每页查询的个数
                               @RequestParam String username,
                               @RequestParam String name){
        Page<User> page = userService.selectByPage(pageNum, pageSize, username, name);
        return Result.success(page);
    }
}
